package com.spider.site.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.spider.site.entity.SpiderResult;
import com.spider.site.pojo.ResponseBean;
import com.spider.site.pojo.util.MSG;
import com.spider.site.service.SpiderService;

import reactor.core.publisher.Mono;

/**
 * 蜘蛛侠的中枢大脑，负责和人类聊天，数据出炉
 * @author wq
 *
 */
@RestController
@RequestMapping("/spider")
public class SpiderController {

	
	@Autowired
	private SpiderService spiderService;
	
	/**
	 * 获取所有爬取的 url
	 * @return
	 */
	@GetMapping("/getAllUrl")
	public Mono<Object> getAllUrl() {
		return Mono.just(new ResponseBean(MSG.STATUS_200 , MSG.FIND_OK , spiderService.getAllUrl()));
	}
	
	/**
	 * 添加任务
	 * @param addurl
	 * @return
	 */
	@PostMapping("/addSpider")
	public Mono<Object> addSpider(SpiderResult spider){
		if(spider == null || spider.getUrl() == null || spider.getUrl().equals("")) {
			return Mono.just(new ResponseBean(MSG.STATUS_400 , MSG.PARAM_ERR));
		}
		boolean ok = this.spiderService.addUrl(spider);
		if(ok) 
			return Mono.just(new ResponseBean(MSG.STATUS_200 , MSG.ADD_OK));
		else
			return Mono.just(new ResponseBean(MSG.STATUS_500 , MSG.ADD_FAIL));
	}
	
	/**
	 * 编辑 爬虫
	 * @param spider
	 * @return
	 */
	@PostMapping("/editSpider")
	public Mono<Object> editSpider(SpiderResult spider){
		if(spider == null || spider.getUrl() == null || spider.getUrl().equals("")) {
			return Mono.just(new ResponseBean(MSG.STATUS_400 , MSG.PARAM_ERR));
		}
		boolean ok = this.spiderService.editUrl(spider);
		if(ok) 
			return Mono.just(new ResponseBean(MSG.STATUS_200 , MSG.EDIT_OK));
		else
			return Mono.just(new ResponseBean(MSG.STATUS_500 , MSG.EDIT_FAIL));
	}
	
	/**
	 * 删除 爬虫
	 * @param ids
	 * @return
	 */
	@GetMapping("/removeSpider")
	public Mono<Object> removeSpider(long[] ids){
		if(ids == null || ids.length <= 0) {
			return Mono.just(new ResponseBean(MSG.STATUS_400 , MSG.PARAM_ERR));
		}
		boolean ok = this.spiderService.removeUrl(ids);
		if(ok) 
			return Mono.just(new ResponseBean(MSG.STATUS_200 , MSG.DEL_OK));
		else
			return Mono.just(new ResponseBean(MSG.STATUS_500 , MSG.DEL_FAIL));
	}
	
	/**
	 * 查询任务是否相同
	 * @param addurl
	 * @return
	 */
	@GetMapping("/checkURL")
	public Mono<Object> checkURL(String addurl){
		if(addurl == null || addurl.equals("")) {
			return Mono.just(new ResponseBean(MSG.STATUS_400 , MSG.PARAM_ERR));
		}
		boolean ok = this.spiderService.checkURL(addurl);
		if(ok) 
			return Mono.just(new ResponseBean(MSG.STATUS_200 , MSG.FIND_OK, ok));
		else
			return Mono.just(new ResponseBean(MSG.STATUS_500 , "添加失败, 可能该地址已经添加，可能该地址不合法"));
	}
	
	/**
	 * 获取所有爬虫任务
	 * @return
	 */
	@GetMapping("/getAllSpiders")
	public Mono<Object> getAllSpiders(){
		return Mono.just(new ResponseBean(MSG.STATUS_200 , MSG.FIND_OK , this.spiderService.getAll()));
	}
	
	/**
	 * 通过id查找到爬虫
	 * @param id
	 * @return
	 */
	@GetMapping("/getSpiderById")
	public Mono<Object> getSpiderById(long id){
		if(id <= 0) {
			return Mono.just(new ResponseBean(MSG.STATUS_400 , MSG.PARAM_ERR));
		}
		SpiderResult spiderResult = this.spiderService.getById(id);
		if(spiderResult != null) {
			return Mono.just(new ResponseBean(MSG.STATUS_200 , MSG.FIND_OK , spiderResult));
		}else {
			return Mono.just(new ResponseBean(MSG.STATUS_500 , MSG.FIND_FAIL));
		}
		
	}
	
	/**
	 * 是否开启爬取（开启爬取之后，调度扫描到之后，将开始不停歇的爬取，直到关闭此爬虫为止）
	 * @param spin
	 * @return
	 */
	@GetMapping("/toSpin")
	public Mono<Object> toSpin(int spin, Long id){
	    if(id == null || id <= 0) {
	    	return Mono.just(new ResponseBean(MSG.STATUS_400 , MSG.PARAM_ERR));
	    }
		boolean ok = this.spiderService.spinResult(spin, id);
		if(ok) {
			return Mono.just(new ResponseBean(MSG.STATUS_200 , "开启或停止爬虫成功" , ok));
		}else {
			return Mono.just(new ResponseBean(MSG.STATUS_500 , MSG.EDIT_FAIL));
		}
	} 
	
	/**
	 * 查看信息时，查看一些历史抓取的信息
	 * @param id
	 * @return
	 */
	@GetMapping("/getDates")
	public Mono<Object> getDates(long id){
		if(id <= 0) {
			return Mono.just(new ResponseBean(MSG.STATUS_400 , MSG.PARAM_ERR));
		}
		return Mono.just(new ResponseBean(MSG.STATUS_200 , MSG.FIND_OK , this.spiderService.getDateBySpiderResultId(id)));
	}
	
	/**
	 * 获取 对应的评论区
	 * @param id
	 * @param history
	 * @return
	 */
	@GetMapping("/findHistory")
	public Mono<Object> findHistory(long id , String history){
		if(id <= 0 || history == null || history.equals("")) {
			return Mono.just(new ResponseBean(MSG.STATUS_400 , MSG.PARAM_ERR));
		}
		return Mono.just(new ResponseBean(MSG.STATUS_200 , MSG.FIND_OK , this.spiderService.getHistoryResult(id, history)));
	}
}
